/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.graphs;

import java.util.List;
import org.osdtsystem.functions.LocalFunction;
import org.osdtsystem.graphs.views.ViewID;
import org.osdtsystem.values.EdgeID;
import org.osdtsystem.values.Feature;
import org.osdtsystem.values.Layer;
import org.osdtsystem.values.LocalProperty;
import org.osdtsystem.values.Location;
import org.osdtsystem.values.NodeID;
import static org.osdtsystem.values.Constants.*;

/**
 * This class provides a function with limited access to a graph. Ie, the function
 * is not allowed to change anything in the graph, and the function must pass
 * on a localfunction reference to itself so that the functional dependency can be
 * registered correctly (failure to do so will result in incorrect updating).
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class GraphAccessorEngine implements GraphAccessor {
    private Graph graph;

    public GraphAccessorEngine(Graph graph) {
        this.graph = graph;
    }

    @Override
    public Layer namedLayer(LocalFunction<?,?> caller, String uniqueName) {
        return graph.lookupLayer(uniqueName);
    }

    @Override
    public ViewID namedView(LocalFunction caller, String uniqueName) {
        return graph.viewID(uniqueName);
    }

    @Override
    public List<EdgeID> inEdges(LocalFunction caller, NodeID node, Layer layer) {
        graph.addCaller(caller, node, layer, INEDGES);
        return graph.inEdges(node, layer);
    }

    @Override
    public List<EdgeID> outEdges(LocalFunction caller, NodeID node, Layer layer) {
        graph.addCaller(caller, node, layer, OUTEDGES);
        return graph.outEdges(node, layer);
    }

    @Override
    public NodeID inNode(LocalFunction caller, EdgeID edge) {
        graph.addCaller(caller, edge, EDGE);
        return graph.inNode(edge);
    }

    @Override
    public NodeID outNode(LocalFunction caller, EdgeID edge) {
        graph.addCaller(caller, edge, EDGE);
        return graph.outNode(edge);
    }

    @Override
    public Layer layer(LocalFunction caller, Location loc) {
        return graph.layer(loc);
    }

    @Override
    public Object featureValue(LocalFunction<?,?> caller, 
            Location location, Feature feature) {
        graph.addCaller(caller, new LocalProperty(location, feature));
        return graph.featureValue(location, feature);
    }

    @Override
    public Object functionValue(LocalFunction<?, ?> caller, Location loc, String functionName) {
        graph.addCaller(caller, new LocalProperty(loc, graph.lookupFunction(functionName)));
        return graph.functionValue(loc, functionName);
    }
}
